package com.mrobuy.jd.apply;

import com.gavel.database.SQLExecutor;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.type.ApplyState;
import com.mrobuy.jdvc.entity.VCProductApply;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class VCProductAppliesLoader {

    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws Exception {

        load(JdClientFactory.jdClient(), null);
    }

    public static void load(JdClient client, Date beginTime) throws Exception {

        List<VCProductApply> productApplies = Jos.productApplyFindClient(client)
                .setBeginTime("2021-03-01")
                .call();

        for (VCProductApply apply : productApplies) {

            ApplyState applyState = ApplyState.getStateByCode(apply.getState());
            apply.setReject(applyState.reject());
            apply.setZt(1);
            VCProductApply existApply = SQLExecutor.executeQueryBean("select * from VC_PRODUCT_APPLY where APPLYID = ?", VCProductApply.class, apply.getApplyId());
            if ( existApply == null ) {
                existApply = apply;
                SQLExecutor.insert(existApply);
                continue;
            }

            if ( existApply.getState() != apply.getState() ) {
                existApply.setOpinion(" ");
            }
            if ( apply.getState() == ApplyState.STATE_302.code() ) {
                existApply.setPriceexport("N");
            }

            SQLExecutor.update(apply);
        }
    }


}
